package org.xhnews.utils;

import net.rim.device.api.io.transport.ConnectionFactory;
import net.rim.device.api.io.transport.TransportInfo;
import net.rim.device.api.system.WLANInfo;

public class NetworkUtil {

	private static ConnectionFactory connFact = new ConnectionFactory();
	/*
	 * 初始化网络传输类型列表
	 */
	static {
		int[] TransportTypes = { TransportInfo.TRANSPORT_TCP_WIFI,
				TransportInfo.TRANSPORT_TCP_CELLULAR,
				TransportInfo.TRANSPORT_WAP2, TransportInfo.TRANSPORT_WAP,
				TransportInfo.TRANSPORT_MDS, TransportInfo.TRANSPORT_BIS_B };
		connFact.setPreferredTransportTypes(TransportTypes);
	}

	/*
	 * 获得系统可用的网络传输类型
	 */
	public static String[] getAvailableTransportTypes() {
		int TransportTypes[] = TransportInfo.getAvailableTransportTypes();
		String[] sTmp = new String[TransportTypes.length];
		for (int i = 0; i < TransportTypes.length; i++) {
			int tt = TransportTypes[i];
			switch (tt) {
			case TransportInfo.TRANSPORT_BIS_B:
				sTmp[i] = "BIS-B";
				break;
			case TransportInfo.TRANSPORT_MDS:
				sTmp[i] = "MDS";
				break;
			case TransportInfo.TRANSPORT_TCP_CELLULAR:
				sTmp[i] = "TCP CELLULAR";
				break;
			case TransportInfo.TRANSPORT_TCP_WIFI:
				sTmp[i] = "WIFI";
				break;
			case TransportInfo.TRANSPORT_WAP:
				sTmp[i] = "WAP 1.0 OR 1.1";
				break;
			case TransportInfo.TRANSPORT_WAP2:
				sTmp[i] = "WAP 2.0";
				break;
			}
		}
		return sTmp;
	}

	/*
	 * 获得网络传输类型选择优先列表
	 */
	public static int[] getPreferredTransportTypes() {
		return connFact.getPreferredTransportTypes();
	}
	/*
	 * 获得连接工厂
	 */
	public static ConnectionFactory getConnectionFactory(){
		return connFact;
	}

	public static boolean isWifiAvailable() {
		if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED)
			return true;
		else
			return false;
	}

}
